package org.edu.modules.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.edu.modules.common.entity.VeBaseJYZ;
import org.edu.modules.common.mapper.VeBaseJYZMapper;
import org.edu.modules.common.service.IVeBaseJYZService;
import org.edu.modules.common.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther 李少君
 * @Date 2021-12-13 16:26
 */
@Service
public class VeBaseJYZServiceImpl extends ServiceImpl<VeBaseJYZMapper, VeBaseJYZ> implements IVeBaseJYZService {

    @Autowired
    private VeBaseJYZMapper veBaseJYZMapper;

    @Override
    public List<Map<String, Object>> getJYZTreeList() {
        List<Map<String, Object>> rootList = this.veBaseJYZMapper.getJYZRootList();
        List<Map<String, Object>> bodyList = this.veBaseJYZMapper.getJYZBodyList();
        //rootList = setJlnyName(rootList);
        if (bodyList != null && !bodyList.isEmpty()) {
            Map<String, String> map = new HashMap<>();
            rootList.forEach(jyz -> getChild(jyz, map));
            return rootList;
        }
        return null;
    }

    @Override
    public VeBaseJYZ getJYZByName(Integer id, String name) {
        VeBaseJYZ veBaseJYZ = this.veBaseJYZMapper.getJYZByName(id, name);
        /*if (veBaseJYZ != null)
            if (veBaseJYZ.getJlny() != null) {
                veBaseJYZ.setJlnyName(DateTimeUtil.timestampToDate(veBaseJYZ.getJlny().intValue()));
            } else {
                veBaseJYZ.setJlnyName("");
            }*/
        return veBaseJYZ;
    }

    @Override
    public VeBaseJYZ getJYZByCode(Integer id, String code) {
        VeBaseJYZ veBaseJYZ = this.veBaseJYZMapper.getJYZByCode(id, code);
        /*if (veBaseJYZ != null)
            if (veBaseJYZ.getJlny() != null) {
                veBaseJYZ.setJlnyName(DateTimeUtil.timestampToDate(veBaseJYZ.getJlny().intValue()));
            } else {
                veBaseJYZ.setJlnyName("");
            }*/
        return veBaseJYZ;
    }

    @Override
    public Map getJYZById(Integer jyzId) {
        Map<String, String> map = this.veBaseJYZMapper.getJYZById(jyzId);
        /*if (map != null)
            if (!"".equals(map.get("jlny")) && map.get("jlny") != null) {
                Long times = Long.valueOf(Long.parseLong(map.get("jlny").toString()));
                map.put("jlnyName", DateTimeUtil.timestampToDate(times.longValue()));
            } else {
                map.put("jlnyName", "");
            }*/
        return map;
    }

    public void getChild(Map<String, Object> mapModel, Map<String, String> map) {
        List<Map<String, Object>> bodyList = this.veBaseJYZMapper.getJYZBodyList();
        List<Map<String, Object>> children = new ArrayList<>();
        //bodyList = setJlnyName(bodyList);
        bodyList.stream().filter(c -> !map.containsKey(c.get("id"))).filter(c -> c.get("pid").toString().equals(mapModel.get("id").toString()))
                .forEach(c -> {
                    map.put(c.get("id").toString(), c.get("pid").toString());
                    getChild(c, map);
                    children.add(c);
                });
        mapModel.put("children", children);
    }

    private List<Map<String, Object>> setJlnyName(List<Map<String, Object>> list) {
        if (list.size() > 0)
            for (Map<String, Object> map : list) {
                if (!"".equals(map.get("jlny")) && map.get("jlny") != null) {
                    Long times = Long.valueOf(Long.parseLong(map.get("jlny").toString()));
                    map.put("jlnyName", DateTimeUtil.timestampToDate(times.longValue()));
                    continue;
                }
                map.put("jlnyName", "");
            }
        return list;
    }
}
